package com.molichuxing.services.infrastructure.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.StockBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.StockMapper;
import com.molichuxing.services.infrastructure.dto.request.create.StockCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.StockModifyDto;
import com.molichuxing.services.infrastructure.dto.response.StockDto;
import com.molichuxing.services.infrastructure.service.StockService;
/**
 * 
 * @author liyg
 * 仓库服务实现类
 * 2019-08-20
 *
 */
@Service("stockService")
public class StockServiceImpl implements StockService{

	@Autowired
	private StockMapper stockMapper;
	
	/**
	 * 获取仓库详情
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public StockDto getStock(Integer id) {
		
		StockBean stockBean = stockMapper.getById(id);
		StockDto stockDto = Convert.toStockDto(stockBean);
		
		return stockDto;
	}
	
	/**
	 * 获取仓库详情
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	public Map<Integer,StockDto> getStocks(List<Integer> ids) {
		
		if(null == ids || ids.size() <= 0) {
			return null;
		}
		
		List<StockBean> list=stockMapper.getByIds(ids);
		List<StockDto> stockDtoList= Convert.toStockDto(list);
		if(null == stockDtoList || stockDtoList.isEmpty()) {
			return null;
		}
		
		int initialCapacity=stockDtoList.size();
		Map<Integer,StockDto> map=new  HashMap<>(initialCapacity);
		for (StockDto stockDto : stockDtoList) {
			map.put(stockDto.getId(), stockDto);
		}
		
		return map;
	}
	
	/**
	 * 保存仓库信息
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	public Integer create(StockCreateDto createDto) {
		
		StockBean stockBean=new StockBean();
		BeanUtils.copyProperties(createDto, stockBean);
		
		stockMapper.create(stockBean);
		
		return stockBean.getId();
	}
	
	/**
	 * 修改仓库信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modify(StockModifyDto modifyDto) {
		
		StockBean stock = stockMapper.getById(modifyDto.getId());
		if(null == stock) {
			throw new IllegalArgumentException("不存在该条信息");
		}
		
		StockBean stockBean=new StockBean();
		BeanUtils.copyProperties(modifyDto, stockBean);
		
		int num = stockMapper.updateById(stockBean);
		
		return num== 1 ? true : false;
	}

}
